package cloudnative.demo.labservice.controller;

import cloudnative.demo.labservice.mapper.LaboratoryMapper;
import cloudnative.demo.labservice.model.Laboratory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/laboratory")
@AllArgsConstructor
@Slf4j
public class LabController {

    @Autowired
    private LaboratoryMapper laboratoryMapper;


    // 查询所有实验室信息
    @GetMapping("/queryAll")
    public List<Laboratory> queryAllLaboratories() {
        return laboratoryMapper.queryAllLaboratories();
    }

    // 通过WorkerID查询实验室信息
    @PostMapping("/queryML")
    public List<Laboratory> queryLaboratoriesByWorkerID(@RequestBody Laboratory laboratory) {
        Long workerID = laboratory.getWorkerID();
        if (workerID == null) {
            return laboratoryMapper.queryAllLaboratories();
        } else {
            return laboratoryMapper.queryLaboratoriesByWorkerID(workerID);
        }
    }

    // 添加实验室
    @PostMapping("/addL")
    public void addLaboratory(@RequestBody Laboratory laboratory) {
        laboratoryMapper.addLaboratory(laboratory.getWorkerID());
    }

    // 删除实验室
    @PostMapping("/deleteL")
    public void deleteLaboratory(@RequestBody Laboratory laboratory) {
        laboratoryMapper.deleteLaboratory(laboratory.getLaboratoryID());
    }

    // 更新实验室信息
    @PostMapping("/alterL")
    public void updateLaboratory(@RequestBody Laboratory laboratory) {
        laboratoryMapper.updateLaboratory(laboratory.getLaboratoryID(), laboratory.getWorkerID());
    }
}
